package com.op.studyingliteapp.fragment.notes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.op.studyingliteapp.R;
import com.op.studyingliteapp.activity.NoteAddActivity;
import com.op.studyingliteapp.bean.NoteBean;
import com.op.studyingliteapp.common.Common;
import com.op.studyingliteapp.database.dao.NoteDao;
import com.op.util.SharedPreferenceUtil;

public class NotesFragment extends Fragment {
	private static final String TAG = NotesFragment.class.getSimpleName();
	private String getNoteUserId, getNoteCourseId;
	private ListView notesListView;
	private List<NoteBean> notesContentList;
	private NotesAdapter adapter;
	ArrayList<NoteData> noteList = new ArrayList<NoteData>();

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_notes, container, false);
		notesListView = (ListView) root
				.findViewById(R.id.fragment_notes_listview);
		notesContentList = new ArrayList<NoteBean>();

		SharedPreferences preferences = getActivity().getSharedPreferences(
				"StudyingLite", Context.MODE_PRIVATE);
		getNoteUserId = preferences.getString(Common.Prefs.USER_ID, "");
		getNoteCourseId = preferences.getString(Common.Prefs.COURSE_ID, "");

		NoteDao noteDao = new NoteDao(getActivity(), getNoteUserId);
		notesContentList = noteDao.getNotifByCourseIdAndUserId(getNoteCourseId,
				getNoteUserId);

		adapter = new NotesAdapter(getActivity(), notesContentList);
		notesListView.setAdapter(adapter);
		// listView click change note
		notesListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, Object> prefsMap = new HashMap<String, Object>();
				prefsMap.put(Common.Prefs.NOTE_ID, adapter.getNoteId(position));
				prefsMap.put(Common.Prefs.NOTE_CHANGE, "true");
				prefsMap.put(Common.Prefs.NOTE_CONTENT,
						adapter.getNoteContent(position));
				SharedPreferenceUtil.saveData(getActivity(), prefsMap);
				Intent intent = new Intent();
				intent.setClass(getActivity(), NoteAddActivity.class);
				startActivity(intent);
			}
		});

		/**
		 * 长按删除
		 */
		notesListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, Object> prefsMap = new HashMap<String, Object>();
				prefsMap.put(Common.Prefs.NOTE_ID, adapter.getNoteId(position));
				prefsMap.put(Common.Prefs.NOTE_CHANGE, "true");
				prefsMap.put(Common.Prefs.NOTE_CONTENT,
						adapter.getNoteContent(position));
				SharedPreferenceUtil.saveData(getActivity(), prefsMap);

				AlertDialog.Builder builder = new Builder(getActivity());
				builder.setMessage("确认删除该笔记吗？\n");
				builder.setTitle("提示");
				builder.setNeutralButton("确认",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								SharedPreferences preferences = getActivity()
										.getSharedPreferences("StudyingLite",
												Context.MODE_PRIVATE);
								String getUserId = preferences.getString(
										Common.Prefs.USER_ID, "");// 获得用户Id
								String noteId = preferences.getString(
										Common.Prefs.NOTE_ID, "");// 获取笔记Id

								NoteDao noteDao = new NoteDao(getActivity(),
										getUserId);
								noteDao.deleteNotifBynoteId(noteId);
								Map<String, Object> prefsMap = new HashMap<String, Object>();
								prefsMap.put(Common.Prefs.NOTE_CHANGE, "false");
								SharedPreferenceUtil.saveData(getActivity(),
										prefsMap);

								notesContentList = noteDao
										.getNotifByCourseIdAndUserId(
												getNoteCourseId, getNoteUserId);
								adapter = new NotesAdapter(getActivity(),
										notesContentList);
								notesListView.setAdapter(adapter);
							}
						});
				builder.setNegativeButton("取消", null);
				builder.create();
				builder.show();

				return true;
			}
		});

		return root;
	}

	/**
	 * 从数据库获取数据 // 从数据库提取课程讨论主贴内容
	 */
	public void getDataFromDatabase() {
		NoteDao noteDao = new NoteDao(getActivity(), getNoteUserId);
		List<NoteBean> note = noteDao.getNotifByCourseIdAndUserId(
				getNoteCourseId, getNoteUserId);
		noteList.clear();
		// 获取讨论标题
		for (int i = 0; i < note.size(); i++) {
			String content = note.get(i).getContent();
			String courseId = note.get(i).getCourseId();
			String courseName = note.get(i).getCourseName();
			String userId = note.get(i).getUserBean().getId();
			String userName = note.get(i).getUserBean().getName();
			String userNamber = note.get(i).getUserBean().getNumber();
			String createTime = note.get(i).getCreateTime();
			String userType = note.get(i).getUserBean().getType();
			String avatar = note.get(i).getUserBean().getImageUrl();

			/**
			 * 依次是内容，课程Id,课程名称，用户ID，用户名字,用户工号,笔记创建时间,用户类型，用户头像地址
			 */
			noteList.add(new NoteData(content, courseId, courseName, userId,
					userName, userNamber, createTime, userType, avatar));
		}
	}

	@Override
	public void onStart() {
		// 初始化笔记是否更改的记录
		Map<String, Object> prefsMap = new HashMap<String, Object>();
		prefsMap.put(Common.Prefs.NOTE_CHANGE, "false");
		SharedPreferenceUtil.saveData(getActivity(), prefsMap);
		super.onStart();
	}

	@Override
	public void onResume() {
		NoteDao noteDao = new NoteDao(getActivity(), getNoteUserId);
		notesContentList = noteDao.getNotifByCourseIdAndUserId(getNoteCourseId,
				getNoteUserId);
		adapter = new NotesAdapter(getActivity(), notesContentList);
		notesListView.setAdapter(adapter);
		setHasOptionsMenu(true);//设置顶栏可见
		super.onResume();

	}


	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		getActivity().getMenuInflater().inflate(R.menu.note_send, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sendnote:
			Intent intent = new Intent();
			intent.setClass(getActivity(), NoteAddActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
